<template>
  <div>
    <div class="refundApply">
      <header class="mint-header is-fixed">
        <div class="mint-header-button is-left">
          <button class="mint-button mint-button--default mint-button--normal">
            <span class="mint-button-icon">
              <i class="mintui mintui-back" @click="$router.go(-1)"></i>
            </span>
          </button>
        </div>
        <h1 class="mint-header-title">选择售后数量</h1>
        <div class="mint-header-button is-right">
          <button class="mint-button mint-button--default mint-button--normal">
            <label class="mint-button-text">
              <i class="mint-toast-icon mintui mintui-search"></i>
            </label>
          </button>
        </div>
      </header>
      <div class="title">
        <span style="float:left;margin-right:2.5rem">选择</span>
        <span>规格/单价/数量</span>
        <span style="float:right">已付</span>
      </div>
      <div class="orderGoods">
        <div class="c-order" v-for="(item,index) in orderGoodsList" :key="index" v-if="item.returnId == 0">
          <i class="iconfont" :class="[item.check ? 'icon-select' :'icon-select-default']" @click="checked(item,index)"></i>
          <div class="order-mid">
            <p>{{item.sellingPoints}}</p>
            <span>￥{{item.goodsMoney|formatMoney}}x{{item.goodsNum}}</span><br>
            <span style="margin-right:10px;">货号：{{item.goodsCode}}</span>
            <span v-if="!item.sizeAlias">尺码：{{item.goodsSpec}}</span>
            <span v-if="item.sizeAlias">尺码：{{item.goodsSpec}}({{item.sizeAlias}})</span>
          </div>
          <img :src="item.goodsImg" alt="" class="pic">
          <div class="order-right">￥{{item.goodsMoney*item.goodsNum}}</div>
        </div>
      </div>
      <div class="bot-info">
        <span @click="selectedAllGoods">
          <i class="iconfont" :class="[selectedAll ? 'icon-select' : 'icon-select-default']"></i>全选</span>
        <p>选择货品数量：{{totalNum}}</p>
        <p>可退货品金额：￥{{totalFee|formatMoney}}</p>
      </div>
      <div class="apply">
        <button @click="apply">确认售后货品</button>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getOrderDetails
} from '@/api/shop'
import {
  mapGetters
} from 'vuex'
import { Toast } from 'mint-ui'
export default {
  data() {
    return {
      flag: false,
      check: false,
      selectedAll: false,
      orderGoodsList: [],
      totalFee: 0,
      totalNum: 0,
      orderGoodsId: 0
    }
  },
  watch: {},
  components: {
  },
  created() {
    this.fetchData()
  },
  computed: {
    ...mapGetters({
      userInfo: 'userInfo'
    })
  },
  methods: {
    fetchData() {
      // 判断是否登录
      this.isLogin = this.userInfo ? 1 : 0
      if (!this.userInfo) return
      // 获取购物车信息
      const formData = {
        userId: this.userInfo.id,
        orderId: this.$route.query.orderId
      }
      getOrderDetails(formData).then(res => {
        // this.orderGoodsList = res.data.orderDetails.orderGoodsList
        const list = []
        res.data.orderDetails.orderGoodsList.forEach(size => {
          if (size.returnId === '0') {
            list.push(size)
          }
        })
        this.orderGoodsList = list
        this.orderGoodsList.forEach(size => {
          this.$set(size, 'check', false)
        })
      })
    },
    checked(item, index) { // 选择商品
      item.check = !item.check
      let count = 0
      this.orderGoodsList.forEach(item => {
        if (item.check) return count++
      })
      if (count === this.orderGoodsList.length) {
        this.selectedAll = true
      } else {
        this.selectedAll = false
      }
      this.computedTotalFee()
    },
    selectedAllGoods() { // 选择所有
      this.orderGoodsList.forEach(store => {
        console.log(store)
        store.check = !this.selectedAll
      })
      this.selectedAll = !this.selectedAll
      this.computedTotalFee()
    },
    computedTotalFee() { // 合计费用
      let computedFee = 0
      let computedNum = 0
      let goodsId = ''
      this.orderGoodsList.forEach(store => {
        if (store.check) {
          computedFee += store.goodsNum * parseFloat(store.goodsMoney)
          computedNum += store.goodsNum
          goodsId = goodsId + store.id + ','
        }
      })
      this.orderGoodsId = goodsId
      this.totalFee = computedFee
      this.totalNum = computedNum
    },
    apply() {
      if (this.totalNum === 0) {
        Toast('请选择售后商品')
      } else {
        this.$router.replace({
          name: 'fillRefundInfo',
          query: {
            orderId: this.$route.query.orderId,
            orderGoodsId: this.orderGoodsId
          }
        })
      }
    }
  }
}

</script>
<style lang="scss">
@import '~assets/common/css/mixin.scss';
.refundApply {
  min-height: 100vh;
  padding-top: 45px;
  padding-bottom: 105px;
  background-color: #fff;
  header {
    height: 45px;
    background-color: #FFF;
    color: #000;
    border-bottom: 1px solid #ECECEC;
    z-index: 2!important;
  }
  .title {
    height: 35px;
    line-height: 35px;
    background-color: #fff;
    padding: 0 15px;
    position: fixed;
    z-index: 1;
    width: 100%;
  }
  .orderGoods {
    padding-top: 35px;
    .c-order {
      padding: 10px 70px 10px 130px;
      background-color: rgb(252, 252, 252);
      overflow: hidden;
      margin-bottom: 3px;
      position: relative;
      i {
        position: absolute;
        top: 50%;
        margin-top: -10px;
        left: 15px;
      }
      .pic {
        position: relative;
        float: left;
        width: 70px;
        height: 85px;
        margin-left: -100%;
        left: -85px;
      }
      .order-mid {
        position: relative;
        float: left;
        width: 100%;
        height: 100%; //   line-height: 25px;
        p {
          line-height: 15px;
          word-break: break-all;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        span {
          color: rgb(190, 190, 190);
          margin-top: 10px;
          display: inline-block;
        }
      }
      .order-right {
        position: absolute;
        width: 65px;
        margin-left: -190px;
        text-align: center;
        top: 50%;
        margin-top: -7px;
        right: 0px; // .count {
        //   width: 40px;
        //   height: 81px;
        //   margin: 0 auto;
        //   border-radius: 5px;
        //   border: 1px solid #E4E4E4;
        //   font-size: 14px;
        //   i {
        //     color: #B2B2B2;
        //     font-weight: 700;
        //     font-size: 16px;
        //     display: block;
        //     text-align: center;
        //     height: 27px;
        //     line-height: 27px;
        //   }
        //   span {
        //     display: block;
        //     text-align: center;
        //     height: 27px;
        //     line-height: 27px;
        //   }
        // }
      }
    }
  }
  .bot-info {
    height: 60px;
    width: 100%;
    background-color: #FFF9D7;
    color: #767676;
    text-align: right;
    line-height: 20px;
    padding: 10px 15px;
    position: fixed;
    bottom: 45px;
    span {
      position: absolute;
      left: 15px;
      top: 50%;
      margin-top: -12.5px;
      color: #000;
      line-height: 21px;
      i {
        vertical-align: middle;
        margin-right: 5px;
      }
    }
  }
  .apply {
    button {
      height: 45px;
      width: 100%;
      font-size: 14px;
      line-height: 45px;
      background-color: #F60;
      color: #FFF;
      text-align: center;
      position: fixed;
      bottom: 0;
    }
  }
}
</style>
